import { reqGetGoodsInfo,reqAddOrUpdateShopCart } from "@/api"
import {getUUID} from "@/utils/uuid_token"
const state = {
    goodsInfo: {},
    uuid_token: getUUID()
}
const actions = {
    async getGoodsInfo({commit},skuId) {
        let result = await reqGetGoodsInfo(skuId) 
        if (result.data.code === 200) {
            commit("GETGOODSINFO",result.data.data);
        }
    },    
    async addOrUpdateShopCart(_, { skuId, sumNum }) {//从data中解构出skuId和sumNum
        let result = await reqAddOrUpdateShopCart(skuId, sumNum);
        if (result.data.code === 200) {
            return "ok"
        } else {
            return Promise.reject(new Error("fail"))
        }
    }
}
const mutations = {
    GETGOODSINFO(state,value) {
        state.goodsInfo=value
    }
}
const getters = {
    categoryView(state) {
        return state.goodsInfo.categoryView || {}
    },
    skuInfo(state) {
        return state.goodsInfo.skuInfo || {}
    },
}
export default {
    state,
    actions,
    mutations,
    getters
}